{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from tqdm import tqdm\n",
    "from datetime import datetime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "complete_train_path = '../data/complete_train.csv'\n",
    "washed_complete_train_path = '../data/washed_complete_train_0.6.csv'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#读唯一路由\n",
    "def get_test_route_set(path):\n",
    "    data = pd.read_csv(path) \n",
    "    test_route_set = data['TRANSPORT_TRACE'].unique()\n",
    "    return test_route_set\n",
    "test_route_set = get_test_route_set(test_data_path)\n",
    "test_route_set\n",
    "#读港口信息，测试路由用到的港口"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "from math import radians, cos, sin, asin, sqrt\n",
    "def haversine(lon1, lat1, lon2, lat2): # 经度1，纬度1，经度2，纬度2 （十进制度数）\n",
    "    # 将十进制度数转化为弧度\n",
    "    lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])\n",
    "    # haversine公式\n",
    "    dlon = lon2 - lon1 \n",
    "    dlat = lat2 - lat1 \n",
    "    a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2\n",
    "    c = 2 * asin(sqrt(a)) \n",
    "    r = 6371 # 地球平均半径，单位为公里\n",
    "    return c * r * 1000\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "order_route_a_data=pd.read_csv(route_order_folder_path+'/CNYTN-RTM.csv'\n",
    "                              , names=['loadingOrder', 'carrierName','timestamp','longitude','latitude','vesselMMSI','speed','direction','TRANSPORT_TRACE'])\n",
    "order_route_a_data.insert(6,'speed_count',0)\n",
    "\n",
    "#按照订单分组\n",
    "order_route_a_data['timestamp'] = pd.to_datetime(order_route_a_data['timestamp'], infer_datetime_format=True)\n",
    "a_order_route_a_data=order_route_a_data.groupby('loadingOrder')\n",
    "\n",
    "\n",
    "front_lon=0\n",
    "front_lat=0\n",
    "front_timestamp=datetime.now()\n",
    "print(front_timestamp)\n",
    "for order,gps_information in tqdm(a_order_route_a_data):  \n",
    "    row_number=gps_information.shape[0]#row number\n",
    "    count=row_number\n",
    "    for date, row in gps_information.iterrows(): \n",
    "        now_lon=row.longitude\n",
    "        now_lat=row.latitude\n",
    "        now_timestamp=row.timestamp\n",
    "        if count!=row_number:\n",
    "            time=(now_timestamp-front_timestamp).seconds#by seconds\n",
    "            if time==0:\n",
    "                time=1\n",
    "            distance=haversine(front_lon, front_lat, now_lon, now_lat)#by meter\n",
    "            row.speed_count=int(distance*3.6/time)\n",
    "            print( row.loadingOrder,row.timestamp,row.speed_count ,row.speed ,row.speed_count-row.speed)\n",
    "            \n",
    "        front_lon=now_lon\n",
    "        front_lat=now_lat\n",
    "        front_timestamp=now_timestamp\n",
    "        count-=1\n"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "order_route_a_data[order_route_a_data['speed']!=order_route_a_data['speed_count']]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    " haversine(-6.184763, 53.343532, -6.200863, 53.343467)\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
